from elasticsearch import Elasticsearch

from schemas.blog_elastic import BLOG_MAPPING
from config.logging import logger

ES_HOSTS = "http://14.103.235.151:9200"
ES_USER = "elastic"
ES_PASSWORD = "19940822Cxx"
BLOG_INDEX = "blog"

# 注意Python的elasticsearch的版本要和服务器的一致
es = Elasticsearch(ES_HOSTS, http_auth=(ES_USER, ES_PASSWORD))

if not es.indices.exists(index=BLOG_INDEX):
    es.indices.create(index=BLOG_INDEX, body=BLOG_MAPPING)
    logger.info(f"Elastic索引 {BLOG_INDEX} 创建成功")
